package com.bocai.kelala.ui.travel.set.presenter;

import android.util.Log;

import com.bocai.kelala.bean.MyInvoiceBean;
import com.bocai.kelala.bean.MyInvoiceDetailBean;
import com.bocai.kelala.ui.travel.set.contract.MyInvoiceContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/7/6.
 */

public class MyInvoicePresenter extends MyInvoiceContract.presenter {

    private final MyInvoiceContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public MyInvoicePresenter(MyInvoiceContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void myInvoice(String pageIndex, String pageSize) {
        view.showLoading("");
        api.myInvoice(pageIndex,pageSize).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    MyInvoiceBean bean = new Gson().fromJson(result,MyInvoiceBean.class);
                    view.getMyInvoiceList(bean);
                    Log.e("baseResponse",result);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void openInvoice(String orderId, String InvoiceNumber, String Memo, String AddressId) {
        view.showLoading("");
        api.openInvoice(orderId,InvoiceNumber,Memo,AddressId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    view.openInvoiceSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void invoiceDetail(String orderId) {
        view.showLoading("");
        api.invoiceDetail(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("baseResponse",result);
                    MyInvoiceDetailBean bean = new Gson().fromJson(result,MyInvoiceDetailBean.class);
                    view.getInvoiceDetail(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
